import * as constants from './constants'
import { fromJS } from 'immutable'
import { combineReducers } from 'redux-immutable'
import Cookies from 'js-cookie'
import { TOKEN_KEY } from '../constants'
import { reducer as orderReducer } from '../pages/order/store'

const defaultState = fromJS({
  menuCollapsed: false,
  isLogin: true,
  currentPageName: '',
  breadcrumb: [],
  loginUser: {}
})

const reducer = (state = defaultState, action) => {
  const { type, value } = action
  switch (type) {
    case constants.MENU_COLLAPSED:
      return state.set('menuCollapsed', !state.get('menuCollapsed'))
    case constants.CHANGE_LOGIN_STATUS:
      if (!value) {
        Cookies.remove(TOKEN_KEY)
      }
      return state.set('isLogin', value)
    case constants.SET_BREADCRUMB:
      return state.set('breadcrumb', value)
    case constants.SET_LOGIN_USER:
      return state.set('loginUser', value)
    default:
      return state
  }
}

export default combineReducers({
  common: reducer,
  order: orderReducer
})
